#include "ProcessPool.hpp"

int main()
{
    ProcessPool pp;
    pp.InitProcessPool([](int fd){
    while(true)
    {
        int x = 0;
        std::cout << "子进程被阻塞" << std::endl;
        ssize_t n = read(fd, &x, sizeof(x));
        if(n == sizeof(x))
        {
            std::cout << "子进程被唤醒:" << getpid() << std::endl;
            if(x >= 0 && x < tasks.size())
            {
                tasks[x]();
            }
            else
            {
                std::cerr << "没有该任务" << x << std::endl;
            }
        }
        else if(n == 0)//管道已关闭，子进程该退出了
        {
            std::cout << "子进程退出"<< std::endl;
            break;
        }
        else
        {
            std::cerr << "read fd:" << fd << ",error" << std::endl;
            break;
        }
    } });

    pp.PollingCtrlSubProcess(20);

    pp.WaitSubProcess();

    std::cout << "父进程回收子进程结束，父进程结束" << std::endl;
    return 0;
}